package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WmHandleServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmHandleService {
    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    @Override
    public boolean handle(String string) {

        List<WmSensitive> sensitiveList = wmSensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery().select(WmSensitive::getSensitives));
        List<String> list = sensitiveList.stream().map(sensitive -> sensitive.getSensitives()).collect(Collectors.toList());

        //初始化DFA算法的大的map
        SensitiveWordUtil.initMap(list);

        //通过DFA算法审核内容
        Map<String, Integer> map = SensitiveWordUtil.matchWords(string);
        if (null != map && map.size() > 0) {
            return false;
        }

        return true;
    }
}
